﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ControlsLib.SplashScreen;
using System.Threading;
using ExceptionReporting;

namespace GestionePolveri2
{
    static class Program
    {
        static AutoResetEvent autoEvent;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

                autoEvent = new AutoResetEvent(false);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                SplashScreenManager.Show(new SplashForm());
                SplashScreenManager.UpdateInfo("Caricamento Dati...");

                FrameworkLib.Utilities.Singleton<GestionePolveriLib.Cache.LookUpData>.Instance.DataLoaded += new EventHandler(Instance_DataLoaded2);
                FrameworkLib.Utilities.Singleton<GestionePolveriLib.Cache.LookUpData>.Instance.LoadLookUps();

                autoEvent.WaitOne();

                Application.Run(new GestionePolveri());
            }
            catch (Exception ex)
            {
                
            }
            
            //Application.Run(new TestFacade());
            //Application.Run(new TestReport());
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            ExceptionReporter reporter = new ExceptionReporter();
            reporter.Show(e.Exception);
        }

        private static void Instance_DataLoaded2(object sender, EventArgs e)
        {
            if (e == null)
            {
                //Application.ExitThread();
                //Application.Exit();
                //Environment.Exit(0);
            }
            else
            {
                SplashScreenManager.UpdateInfo("Dati caricati !");

                FrameworkLib.Utilities.Singleton<GestionePolveriLib.Cache.LookUpData>.Instance.DataLoaded -= new EventHandler(Instance_DataLoaded2);

                System.Threading.Thread.Sleep(3000);
                SplashScreenManager.Close();

                autoEvent.Set();

                #region
                //MethodInvoker method = () => CloseSplash();

                //if (InvokeRequired)
                //{
                //    BeginInvoke(method);
                //}
                //else
                //{
                //    method();
                //}
                #endregion
            }
        }

        //private static void CloseSplash()
        //{
        //    // done !
        //    SplashScreenManager.Close();
        //    //System.Threading.Thread.Sleep(3000);
        //}
    }
}
